<?php
/**
 * Created by PhpStorm.
 * User: jim
 * Date: 2016-12-19
 * Time: 10:33
 */

namespace App\Models;

use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use Intervention\Image\Facades\Image;
use Intervention\Image\ImageManagerStatic as Img;

class Doctor extends Model
{
    protected $table = "doctor";

    protected $guarded = ['id'];

    public function checkLogin(Request $request)
    {
        $username = $request->input('username');
        $password = $request->input('password');
        $password = md5($password);
        $ob = $this->where([['username', '=', $username], ['password', '=', $password]])->first();
        if ($ob) {
            return $ob;
        } else {
            return null;
        }

    }

    public function doctorGet()
    {
        $list = $this->get();

        return $list;
    }

    public function photo($request)
    {
        if ($request->hasFile('file')) {
            if ($request->file('file')->isValid()) {
                $file = $request->file('file');
                //dd($file);
                $ext = $file->getClientOriginalExtension();
                $picname = time() . rand(1000, 9999) . '.' . $ext;
                $img = Img::make($file);

                $img->resize(620, null, function ($constraint) {
                    $constraint->aspectRatio();
                    $constraint->upsize();
                });
                $img->save('./upload/doctor/'.$picname);

                if ($file->getError() > 0) {
                    return null;
                } else {
                    return $picname;

                }

            }
        }

    }

    public function upinfo(Request $request, $id)
    {
        if ($request->hasFile('xphoto')) {
            if ($request->file('xphoto')->isValid()) {
                $file = $request->file('xphoto');
                $ext = $file->getClientOriginalExtension();
                $picname = time() . rand(1000, 9999) . '.' . $ext;
                $file->move('upload/doctor', $picname);
                if ($file->getError() > 0) {
                    return null;
                }
            }
        }
        $data = $request->only('name', 'sex', 'age', 'photo');
        if (isset($picname)) {
            if ($data['photo'] != 'doc_default.jpg') {
                unlink("upload/doctor/{$data['photo']}");
            }
            Image::make('upload/doctor/'.$picname)->resize('200','200')->save('upload/doctor/'.$picname);
            $data['photo'] = $picname;
        }
        $row = $this->where('id', $id)->update($data);
        if ($row) {
            return $row;
        } else {
            return null;
        }
    }

    public function upcred(Request $request, $id)
    {
        if ($request->hasFile('xphoto')) {
            if ($request->file('xphoto')->isValid()) {
                $file = $request->file('xphoto');
                $ext = $file->getClientOriginalExtension();
                $picname = time() . rand(1000, 9999) . '.' . $ext;
                $file->move('upload/doctor', $picname);
                if ($file->getError() > 0) {
                    return null;
                }
            }
        }
        $data = $request->only('credname', 'crednum', 'credimg');


        if (isset($picname)) {
            if ($data['credimg'] != 'cred_default.jpg') {
                unlink("upload/doctor/{$data['credimg']}");
            }
           // Image::make('upload/doctor/'.$picname)->resize('200','200')->save('upload/doctor/'.$picname);
            $data['credimg'] = $picname;
        }
        $row = $this->where('id', $id)->update($data);
        if ($row) {
            return $row;
        } else {
            return null;
        }
    }



}